module Array where

import qualified Data.Map as Map
import Data.List


data Array a = Array Integer (Map.Map Integer a)

instance Show a => Show (Array a) where
    show (Array _ m) = "{" ++ intercalate ", " (map (show . snd) $ Map.toList m) ++ "}"


get :: Integer -> Array a -> Maybe a
------------------------------------
get idx (Array _ m) = Map.lookup idx m


validIndex :: Integer -> Array a -> Bool
----------------------------------------
validIndex idx (Array len _) = 0 <= idx && idx < len


set :: Integer -> a -> Array a -> Maybe (Array a)
-------------------------------------------------
set idx v arr@(Array len m)
    | validIndex idx arr     = Just $ Array len $ Map.insert idx v m
    | otherwise              = Nothing


length :: Array a -> Integer
----------------------------
length (Array len _) = len


create :: Integer -> a -> Array a
---------------------------------
create len v = Array len $ Map.fromList [ (idx, v) | idx <- [ 0..len-1 ] ]
